package com.xja.controller;


import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.xja.entity.Bookinfo;
import com.xja.entity.Orderinfo;
import com.xja.entity.PageResult;
import com.xja.entity.Userinfo;
import com.xja.feignclient.BookService;
import com.xja.feignclient.UserService;
import com.xja.service.OrderinfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 周广瑞
 * @date 2022/4/26 11:31
 */
@RestController
@RequestMapping("order")
//@CrossOrigin(allowCredentials = "true")
public class OrderController {
    private Logger logger= LoggerFactory.getLogger(OrderController.class);
    @Autowired
    private OrderinfoService orderinfoService;


    @Autowired
    private BookService bookService;
    @Autowired
    private UserService userService;
    @RequestMapping("find")
//    @SentinelResource(value = "timeSentinel",blockHandler = "timeSentinel1")
    public PageResult<List<Bookinfo>>findall(){
            logger.info("调用了订单业务查询所有的信息");
        List<Orderinfo> orderinfoList = orderinfoService.slectOrder();
        for (Orderinfo orderinfo : orderinfoList) {
            System.out.println("orderinfo"+orderinfo);
        }
        return PageResult.success("成功",orderinfoList);
    }
    public PageResult<List<Bookinfo>>timeSentinel1(BlockException blockException){
        logger.info("限流中方法调用了");

        return PageResult.error("限流中，，，请稍等",null);
    }


    //根据userid查询对应的订单
    @RequestMapping("finduid")
    public PageResult<List<Orderinfo>>findUid(@RequestParam("uid") Integer uid){
        logger.info("查询用户的所有订单uid");
        List<Orderinfo> orderinfoList = orderinfoService.selectByUid(uid);
        return PageResult.success("成功",orderinfoList);
    }



    //根据图书id查对应的订单
    @RequestMapping("findbid")
    public PageResult<List<Orderinfo>>FinBid(@RequestParam("bid")Integer bid) {
        logger.info("按照图书id查询相应图书所有的订单");
        List<Orderinfo> orderinfoList = orderinfoService.selectByBookId(bid);
        return PageResult.success("成功",orderinfoList);
    }


    //根据订单id查询显示出用户信息 订单信息 图书信息
    @RequestMapping("findorderandbyoid")
    public PageResult<Map<String,Object>>FindOrderByOid(@RequestParam("oid")Integer oid){
            logger.info("根据订单id查询显示出用户信息 订单信息 图书信息");

        Orderinfo order = orderinfoService.selectOrderByOid(oid);
        if (order==null)
            return PageResult.nullresult("订单不存在",null);

        PageResult<Bookinfo> book = bookService.findBookByBid(order.getBid());
        PageResult<Userinfo> user = userService.findUserByUid(order.getUid());

        Map<String,Object>orderMap = new HashMap<>();
        orderMap.put("Orderinfo",order);
        orderMap.put("Bookinfo",book.getObj());
        orderMap.put("Userinfo",user.getObj());
        return PageResult.success("成功",orderMap);
    }

    @PostMapping("addorder")
    public PageResult<List<Orderinfo>>addorder(Orderinfo orderinfo) {

        logger.info("添加订单");
        orderinfoService.insert(orderinfo);
        return PageResult.success("成功",null);
    }
}
